package com.yygo.jobs;

import com.yygo.model.CreditQuestionnaire;
import com.yygo.model.LoanPerson;
import com.yygo.model.LoanRequest;
import com.yygo.model.Rule;
import com.yygo.service.LoanRequestManager;
import com.yygo.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * Created by tiansha on 2015/7/4.
 */
@Component("D502")
public class D502 implements RuleProcessor {

    @Autowired
    private LoanRequestManager loanRequestManager;

    @Override
    public Rule process(LoanRequest loanRequest) {
        LoanPerson requestPerson = loanRequest.getRequestPerson();
        CreditQuestionnaire requestQuestionnaire = loanRequest.getRequestPersonCreditQuestionnaire();

        boolean same1 = true, same2 = true, same3 = true;
        same1 = compare(requestPerson, requestQuestionnaire);
        
        if (loanRequest.isCoLoaned()) {
            same2 = compare(loanRequest.getCoPerson(), loanRequest.getCoPersonCreditQuestionnaire());
        }
        if (loanRequest.isGuaranted()) {
            same3 = compare(loanRequest.getDbPerson(), loanRequest.getDbPersonCreditQuestionnaire());
        }

        if (!same1 || !same2 || !same3) {
            Rule rule = new Rule("D502", "征信报告与申请信息不一致");
            return rule;
        }

        return null;
    }

    private boolean compare(LoanPerson loanPerson, CreditQuestionnaire creditQuestionnaire) {
        if (loanPerson.getAddress() != null && creditQuestionnaire.getAddress() != null && !loanPerson.getAddress().equals(creditQuestionnaire.getAddress())) {
            return false;
        }

        if (loanPerson.getHkAddress() != null && creditQuestionnaire.getHkAddress() != null && !loanPerson.getHkAddress().equals(creditQuestionnaire.getHkAddress())) {
            return false;
        }

        if (loanPerson.getMaritalStatus() != null && creditQuestionnaire.getMaritalStatus() != null && !loanPerson.getMaritalStatus().equals(creditQuestionnaire.getMaritalStatus())) {
            return false;
        }

        if (!StringUtil.isBlankOrNull(loanPerson.getMobileNo()) && !StringUtil.isBlankOrNull(creditQuestionnaire.getMobileNo()) && !loanPerson.getMobileNo().equals(creditQuestionnaire.getMobileNo())) {
            return false;
        }

        return true;
    }

}